﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;

public partial class UserStorySelection : HierachicalObjectSelection
{
    /// <summary>
    /// Event triggered when user story changes
    /// </summary>
    public event EventHandler SelectedUserStoryChangedEvent;

    /// <summary>
    /// Event triggered when sprint changes
    /// </summary>
    public event EventHandler SelectedSprintChangedEvent;

    /// <summary>
    /// Event triggered when release changes
    /// </summary>
    public event EventHandler SelectedReleaseChangedEvent;

    /// <summary>
    /// Event triggered when project changes
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;

    /// <summary>
    /// Gets the selected UserStory.
    /// </summary>
    /// <value>The selected UserStory.</value>
    public UserStory SelectedUserStory
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListUserStories.SelectedValue) || this.DropDownListUserStories.SelectedValue == "-1")
            {
                return null;
            }
            else
            {
                return DAOUserStory.GetUserStoryByID(Int32.Parse(this.DropDownListUserStories.SelectedValue));
            }
        }
    }

    /// <summary>
    /// Gets the selected user story ID.
    /// </summary>
    /// <value>The selected user story ID.</value>
    public int SelectedUserStoryID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListUserStories.SelectedValue) || this.DropDownListUserStories.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListUserStories.SelectedValue);
            }
        }
    }

    /// <summary>
    /// Holds selected sprint ID
    /// </summary>
    public int SelectedSprintID
    {
        get
        {
            return SprintSelectionUserStory.SelectedSprintID;
        }
    }

    /// <summary>
    /// Holds selected release ID
    /// </summary>
    public int SelectedReleaseID
    {
        get
        {
            return SprintSelectionUserStory.SelectedReleaseID;
        }
    }

    /// <summary>
    /// Holds selected project ID
    /// </summary>
    public int SelectedProjectID
    {
        get
        {
            return SprintSelectionUserStory.SelectedProjectID;
        }
    }

    /// <summary>
    /// Gets the selected sprint.
    /// </summary>
    /// <value>The selected sprint.</value>
    public Sprint GetSelectedSprint()
    {
        return this.SprintSelectionUserStory.SelectedSprint;
    }

    /// <summary>
    /// Gets the selected release.
    /// </summary>
    /// <value>The selected release.</value>
    public Release GetSelectedRelease()
    {
        return this.SprintSelectionUserStory.GetSelectedRelease();
    }

    /// <summary>
    /// Gets the selected project.
    /// </summary>
    /// <value>The selected project.</value>
    public Project GetSelectedProject()
    {
        return this.SprintSelectionUserStory.GetSelectedProject();
    }    

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.SprintSelectionUserStory.SelectedSprintChangedEvent += new EventHandler(SelectedSprintChanged);
        this.SprintSelectionUserStory.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChanged);
        this.SprintSelectionUserStory.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChanged);

        this.SprintSelectionUserStory.SelectedSprintChangedEvent += new EventHandler(SelectedSprintChangedKeeper);
        this.SprintSelectionUserStory.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChangedKeeper);
        this.SprintSelectionUserStory.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChangedKeeper);
    }

    /// <summary>
    /// It keeps projectselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedProjectChangedEvent != null)
            {
                this.SelectedProjectChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps releaseselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedReleaseChangedEvent != null)
            {
                this.SelectedReleaseChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps sprintselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedSprintChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedSprintChangedEvent != null)
            {
                this.SelectedSprintChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the UserStories.
    /// </summary>
    protected void LoadUserStories()
    {
        Sprint s = SprintSelectionUserStory.SelectedSprint;
        if (s == null)
        {
            Release r = SprintSelectionUserStory.GetSelectedRelease();
            if (r == null)
            {
                Project p = SprintSelectionUserStory.GetSelectedProject();
                if (p == null)
                {
                    DropDownListUserStories.DataSource = DAOUserStory.GetAllUserStories();
                }
                else
                {
                    DropDownListUserStories.DataSource = DAOUserStory.GetAllUserStoriesForProject(p.ID);
                }
            }
            else
            {
                DropDownListUserStories.DataSource = DAOUserStory.GetAllUserStoriesForRelease(r.ID);
            }
        }
        else
        {
            DropDownListUserStories.DataSource = DAOUserStory.GetAllUserStoriesForSprint(s.ID);
        }

        DropDownListUserStories.DataTextField = "Name";
        DropDownListUserStories.DataValueField = "ID";
        DropDownListUserStories.DataBind();
        DropDownListUserStories.Items.Insert(0, TopListItemForFilter("Filters","AnyUserStory") );
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        SprintSelectionUserStory.DataBind();
        LoadUserStories();
    }

    /// <summary>
    /// Selected sprint changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedSprintChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadUserStories();
        }
    }

    /// <summary>
    /// Selected release changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadUserStories();
        }
    }

    /// <summary>
    /// Selected project changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadUserStories();
        }
    }

    /// <summary>
    /// Selected UserStory changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedUserStoryChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedUserStoryChangedEvent != null)
            {
                this.SelectedUserStoryChangedEvent(sender, e);
            }
            if (this.SelectedUserStory == null)
            {
                return;
            }

            int selectedUserStoryID = this.SelectedUserStory.ID;
            SprintSelectionUserStory.SetProjectReleaseSprintByUserStory(selectedUserStoryID);

            // Reload UserStories
            LoadUserStories();

            // Select the right UserStory
            for (int i = 0; i < this.DropDownListUserStories.Items.Count; i++)
            {
                if (this.DropDownListUserStories.Items[i].Value == selectedUserStoryID.ToString())
                {
                    this.DropDownListUserStories.SelectedIndex = i;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// Sets the right project, release, sprint and user story in their DDLs
    /// </summary>
    /// <param name="BliID">ID of BLI</param>
    public void SetProjectReleaseSprintUserStoryByBli(int BliID)
    {
        UserStory userStory = DAOUserStory.GetUserStoryByBliID(BliID);

        // Select the right project, release and sprint
        SprintSelectionUserStory.SetProjectReleaseSprintByUserStory(userStory.ID);

        LoadUserStories();

        // Select the right user story
        for (int i = 0; i < this.DropDownListUserStories.Items.Count; i++)
        {
            if (this.DropDownListUserStories.Items[i].Value == userStory.ID.ToString())
            {
                this.DropDownListUserStories.SelectedIndex = i;
                break;
            }
        }
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear(object sender, EventArgs e)
    {
        this.SprintSelectionUserStory.Clear(sender, e);
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListUserStories;
    }

    protected override Label GetLabel()
    {
        return this.LabelUserStories;
    }
}
